﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Entities;
using Utility;
using BUS;

public partial class Administrator_sanpham : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadDropDownList();
            load_data();
        }
    }
    private void LoadDropDownList()
    {
        Tbl_LoaiSP_Collection loaiCollection = Tbl_LoaiSP_BUS.GetAll();
        ddl_loai.DataTextField = Tbl_LoaiSP_Columns.TenLoai.ToString();
        ddl_loai.DataValueField = Tbl_LoaiSP_Columns.STT.ToString();
        ddl_loai.DataSource = loaiCollection;
        ddl_loai.DataBind();
        ddl_loai.Items.Insert(0, new ListItem("All", "0"));
    }
    private void load_data()
    {
        lit_tieude.Text = "Danh sách sản phẩm";
        pager.PageSize = 15;
        Criteria c = new Criteria();
        int total = 0;
        String id = Request.QueryString.ToString();

        if (id.Contains("iddn") == true)
        {
            int id1 = int.Parse(Request.QueryString["iddn"].ToString());
            c.add(Condition.AND, Tbl_SanPham_Columns.DoanhNghiep, Condition.EQUAL,
                id1);
            Tbl_DoanhNghiep_Entities dn = Tbl_DoanhNghiep_BUS.GetByID(id1);
            lit_tieude.Text = "Danh sách sản phẩm thuộc doanh nghiệp </br>-" + dn.TenDN + "-";
        }
         if (id.Contains("id") == true)
        {
            int id1 = int.Parse(Request.QueryString["id"].ToString());
            c.add(Condition.AND, Tbl_SanPham_Columns.LoaiSP, Condition.EQUAL,
                id1);
            Tbl_LoaiSP_Entities lsp = Tbl_LoaiSP_BUS.GetByID(id1);
            lit_tieude.Text = "Danh sách sản phẩm thuộc loại " + lsp.TenLoai;
        }
         if (txt_tensp.Text != "")
         {
             c.add(Condition.AND, Tbl_SanPham_Columns.TenSP, Condition.LIKE,
                 txt_tensp.Text);
         }
        if (ddl_loai.SelectedValue != "0")
        {
            c.add(Condition.AND, Tbl_SanPham_Columns.LoaiSP, Condition.EQUAL,
               ddl_loai.SelectedValue);
        }
     
        Tbl_SanPham_Collection spCollection =
            Tbl_SanPham_BUS.Get(c.Criter, pager.CurrentIndex, pager.PageSize,
            Tbl_SanPham_Columns.STT.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = spCollection;
        Repeater1.DataBind();

        pager.ItemCount = total;
    }
   
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
                 e.Item.ItemType == ListItemType.Item)
        {
            Tbl_SanPham_Entities sp = (Tbl_SanPham_Entities)e.Item.DataItem;
            HyperLink hyp_tensp = (HyperLink)e.Item.FindControl("hyp_tensp");
            Literal lit_masp = (Literal)e.Item.FindControl("lit_masp");
            Literal lit_loai = (Literal)e.Item.FindControl("lit_loai");
            Literal lit_giagoc = (Literal)e.Item.FindControl("lit_giagoc");
            Literal lit_slban = (Literal)e.Item.FindControl("lit_slban");
            HyperLink hyp_tinhtrang = (HyperLink)e.Item.FindControl("hyp_tinhtrang");
            Literal lit_doanhnghiep = (Literal)e.Item.FindControl("lit_doanhnghiep");
            Literal lit_stt = (Literal)e.Item.FindControl("lit_stt");

            hyp_tensp.NavigateUrl = "~/Administrator/sanpham_Detail.aspx?id=" + sp.STT.ToString();
            lit_masp.Text = sp.MaSP;
            lit_giagoc.Text = sp.GiaGoc.ToString();
            lit_slban.Text = sp.SLBan.ToString();
            hyp_tinhtrang.Text = sp.TinhTrang;
            hyp_tinhtrang.NavigateUrl = "~/Administrator/sanpham_suatinhtrang.aspx?id=" + sp.STT.ToString(); ;

            Tbl_LoaiSP_Entities loai = Tbl_LoaiSP_BUS.GetByID(sp.LoaiSP);
            lit_loai.Text = loai.TenLoai;

            Tbl_DoanhNghiep_Entities dn = Tbl_DoanhNghiep_BUS.GetByID(sp.DoanhNghiep);
            lit_doanhnghiep.Text = dn.TenDN;

            lit_stt.Text = sp.STT.ToString();
        }

    }
    protected void bt_timkiem_Click(object sender, EventArgs e)
    {
        load_data();
    }
}
